package cn.myjerry.others.guava;

import java.util.Map;
import java.util.Set;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.common.collect.Table.Cell;

/**
 * 双键的Map --> Table --> rowKey+columnKey+value
 * 1、方法
 * 	所有的行数据：cellSet()
 * 	所有的学生：rowKeySet()
 * 	所有的课程：columnKeySet()
 * 	所有的成绩：values()
 * 	学生对应的课程：rowMap() + get(学生)
 * 					row(学生)
 * 	课程对应的学生：columnMap() + get(课程)
 * 					column(课程)
 * @author Jerry
 *
 */
public class Demo08 {
	public static void main(String[] args) {
		Table<String, String, Integer> table = HashBasedTable.create();
		// 测试数据
		table.put("A", "java", 80);
		table.put("A", "mysql", 60);
		table.put("b", "c++", 70);
		table.put("A", "html", 80);
		table.put("b", "java", 70);
		// 所有的行数据
		Set<Cell<String, String, Integer>> cells = table.cellSet();
		for(Cell<String, String, Integer> temp:cells) {
			System.out.println(temp.getRowKey() + "==>" + temp.getColumnKey() + "==>" + temp.getValue());
		}
		
		System.out.println("++++++++++++学生查看成绩++++++++++++++++");
		System.out.print("学生\t");
		// 所有的课程
		Set<String> cours = table.columnKeySet();
		for(String temp:cours) {
			System.out.print(temp + "\t");
		}
		System.out.println();
		// 所有的学生
		Set<String> stus = table.rowKeySet();
		for(String stu:stus) {
			System.out.print(stu + "\t");
			Map<String, Integer> scores = table.row(stu);
			for(String c:cours) {
				System.out.print(scores.get(c) + "\t");
			}
			System.out.println();
		}
		
		System.out.println("++++++++++++课程查看成绩++++++++++++++++");
		System.out.print("课程\t");
		// 所有学生
		Set<String> stus2 = table.rowKeySet();
		for(String stu:stus2) {
			System.out.print(stu + "\t");
		}
		System.out.println();
		// 所有课程
		Set<String> cours2 = table.columnKeySet();
		for(String temp:cours2) {
			System.out.print(temp + "\t");
			Map<String, Integer> scores2 = table.column(temp);
			for(String s:stus2) {
				System.out.print(scores2.get(s) + "\t");
			}
			System.out.println();
		}
	}
}
